//
//  AuthenMiniMercMsgVM.swift
//  kxf
//
//  Created by Mac on 2024/3/6.
//

import Foundation
import UIKit
import SwiftUI

class AuthenMiniMercMsgVM: UIView {
    
    var selfHeight = RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5
    
    var controller = WHBaseViewVC()
    var mccMsgDict = NSDictionary()
    var mccWechatDict = NSDictionary()
    var mccAlipayDict = NSDictionary()
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var photoTitleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "商户信息"
        return vm
    }()
    lazy var mercNameAllVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.photoTitleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "商户全称"
        vm.textField.placeholder = "请输入商户全称"
        vm.textField.keyboardType = .default
        vm.maxLength = 30
        
        return vm
    }()
    lazy var mercNameSimpleVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mercNameAllVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "商户简称"
        vm.textField.placeholder = "请输入商户简称"
        vm.textField.keyboardType = .default
        return vm
    }()
    lazy var mccVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mercNameSimpleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "行业分类"
        vm.textField.placeholder = "请选择一级分类"
        vm.showArrowImg()
        vm.tapBlock = {()in
            let vc = AuthenMccListVC()
            vc.mccType = "mcc"
            vc.posChannel = AuthenMiniMsgModel.shareInstance.posChannel
            vc.merchantType = "1"
            vc.tapBlock = {(dict)in
                self.mccMsgDict = dict
                self.mccVm.setLabelContent(content: self.mccMsgDict["mccName"]as? String ?? "")
                AuthenMiniMsgModel.shareInstance.mercMccCode = self.mccMsgDict["mcc"]as? String ?? ""
                AuthenMiniMsgModel.shareInstance.mercMccName = self.mccMsgDict["mccName"]as? String ?? ""
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var mccAlipayVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mccVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "支付宝行业"
        vm.textField.placeholder = "请选择支付宝行业"
        vm.showArrowImg()
        vm.isHidden = true
        vm.tapBlock = {()in
            let vc = AuthenMccListVC()
            vc.mccType = "alipay"
            vc.posChannel = AuthenMiniMsgModel.shareInstance.posChannel
            vc.merchantType = "1"
            vc.tapBlock = {(dict)in
                self.mccAlipayDict = dict
                self.mccAlipayVm.setLabelContent(content: self.mccAlipayDict["typeThree"]as? String ?? "")
                AuthenMiniMsgModel.shareInstance.mercAlipayMccCode =
                "\(self.mccAlipayDict["mccCode"]as? String ?? "")"
                AuthenMiniMsgModel.shareInstance.mercAlipayMccName =
                self.mccAlipayDict["typeThree"]as? String ?? ""
                AuthenMiniMsgModel.shareInstance.mercAlipayMcc =
                "\(AuthenMiniMsgModel.shareInstance.mercAlipayMccName)_\(AuthenMiniMsgModel.shareInstance.mercAlipayMccCode)"
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var mccWeChatVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mccVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "微信行业"
        vm.textField.placeholder = "请选择微信行业"
        vm.isHidden = true
        vm.showArrowImg()
        vm.tapBlock = {()in
            let vc = AuthenMccListVC()
            vc.mccType = "wechat"
            vc.merchantType = "1"
            vc.posChannel = AuthenMiniMsgModel.shareInstance.posChannel
            vc.tapBlock = {(dict)in
                self.mccWechatDict = dict
                self.mccWeChatVm.setLabelContent(content: self.mccWechatDict["typeThree"]as? String ?? "")
                AuthenMiniMsgModel.shareInstance.mercWeChatMccCode = "\(self.mccWechatDict["mccId"]as? String ?? "")"
                AuthenMiniMsgModel.shareInstance.mercWeChatMccName = "\(self.mccWechatDict["typeThree"]as? String ?? "")"
                AuthenMiniMsgModel.shareInstance.mercWeChatMcc = "\(AuthenMiniMsgModel.shareInstance.mercWeChatMccName)_\(AuthenMiniMsgModel.shareInstance.mercWeChatMccCode)"
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var areaCodeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mccVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "地区码"
        vm.textField.placeholder = "请选择地区码"
        vm.showArrowImg()
        return vm
    }()
    lazy var addressDetailVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.areaCodeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "详细地址"
        vm.textField.placeholder = "请输入详细地址"
        vm.textField.keyboardType = .default
        vm.maxLength = 40
        return vm
    }()
    lazy var emailVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.addressDetailVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "电子邮箱"
        vm.textField.placeholder = "请输入电子邮箱"
        vm.maxLength = 50
        vm.isHidden = true
        return vm
    }()
    lazy var remarkVm : AuthenRemarkVM = {
        let vm = AuthenRemarkVM.init(frame: CGRect.init(x: 0, y: self.emailVm.frame.maxY, width: 0, height: 0))
        
        vm.isHidden = true
        return vm
    }()
}

extension AuthenMiniMercMsgVM{
    func initUI() {
        addSubview(photoTitleVm)
        addSubview(mercNameAllVm)
        addSubview(mercNameSimpleVm)
        addSubview(mccVm)
        addSubview(mccAlipayVm)
        addSubview(mccWeChatVm)
        addSubview(areaCodeVm)
        addSubview(addressDetailVm)
        addSubview(emailVm)
        addSubview(remarkVm)
        
        if UserModel.shared.posChannel == "fy"{
            self.updateUIShowWeChatMcc()
        }
    }
    
    func updateUIForRefuse() {
        mercNameAllVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.mercNameAll)"
        mercNameSimpleVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.mercNameSimple)"
        mccVm.setLabelContent(content: "\(AuthenMiniMsgModel.shareInstance.mercMccName)")
        mccAlipayVm.setLabelContent(content: "\(AuthenMiniMsgModel.shareInstance.mercAlipayMccName)")
        mccWeChatVm.setLabelContent(content: "\(AuthenMiniMsgModel.shareInstance.mercWeChatMccName)")
        areaCodeVm.setLabelContent(content: "\(AuthenMiniMsgModel.shareInstance.placeCodeText)")
        addressDetailVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.addressDetail)"
        emailVm.textField.text = "\(AuthenMiniMsgModel.shareInstance.emailAddress)"
        if AuthenMiniMsgModel.shareInstance.remark.count > 0{
            remarkVm.textView.text = "\(AuthenMiniMsgModel.shareInstance.remark)"
            remarkVm.placeHolderLabel.isHidden = true
        }
    }
    
    func updateUIShowWeChatMcc() {
        selfHeight = RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*6
        
        let selfFrame = self.frame
        self.frame = CGRect.init(x: selfFrame.origin.x, y: selfFrame.origin.y, width: selfFrame.size.width, height: selfHeight)
        
        mccWeChatVm.isHidden = false
        let areaCodeCenter = areaCodeVm.center
        
        areaCodeVm.center = CGPoint.init(x: areaCodeCenter.x, y: areaCodeCenter.y + areaCodeVm.selfHeight)
        addressDetailVm.center = CGPoint.init(x: areaCodeCenter.x, y: addressDetailVm.center.y + areaCodeVm.selfHeight)
//        emailVm.center = CGPoint.init(x: areaCodeCenter.x, y: emailVm.center.y + areaCodeVm.selfHeight)
//        remarkVm.center = CGPoint.init(x: areaCodeCenter.x, y: remarkVm.center.y + areaCodeVm.selfHeight)
    }
    func setReadMode() {
        self.isUserInteractionEnabled = false
        self.mccVm.arrowImgView.isHidden = true
        self.areaCodeVm.arrowImgView.isHidden = true
    }
}

extension AuthenMiniMercMsgVM{
    @objc func getValue() {
        AuthenMiniMsgModel.shareInstance.mercNameAll = mercNameAllVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.mercNameSimple = mercNameSimpleVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.addressDetail = addressDetailVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.emailAddress = emailVm.textField.text ?? ""
        AuthenMiniMsgModel.shareInstance.remark = remarkVm.textView.text ?? ""
        AuthenMiniMsgModel.shareInstance.dealAddressToPlaceCode()
    }
}
